import java.util.LinkedList;

public class IsCompleteTree {

    public boolean isCompleteTree(TreeNode root) {
        LinkedList<TreeNode> queue = new LinkedList<>();
        boolean flag = false;
        queue.add(root);
        while (!queue.isEmpty()) {
            TreeNode poll = queue.poll();
            if ((poll.right != null && poll.left == null) || (flag && (poll.left != null || poll.right != null))) {
                return false;
            } else {
                if (poll.left != null) queue.add(poll.left);
                if (poll.right != null) queue.add(poll.right);
            }
            if (poll.left == null || poll.right == null) flag = true;
        }
        return true;
    }
}
